package afesocketserver;

import java.net.*;
import java.io.*;

public class GameServerConnection extends Thread {
    protected Socket socket;
    protected BufferedReader socketIn;
    protected PrintWriter socketOut;
    protected GameServer server;
    public String[] clientAddresses = new String[100];
    public int clientAddressesItems = 0;
    public String[] clientData = new String[100];
    public int clientDataItems = 0;

    public GameServerConnection(Socket socket, GameServer server) {
        this.socket = socket;
        this.server = server;
    }

    public SocketAddress getRemoteAddress() {
        return this.socket.getRemoteSocketAddress();
    }

    public String remoteAddrStr;

    protected void debug(String msg) {
        Main.debug("ChatServerConnection (" + this.socket.getRemoteSocketAddress() + ")", msg);
    }

    public void run() {
        try {
            this.socketIn = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
            this.socketOut = new PrintWriter(this.socket.getOutputStream(), true);
            this.remoteAddrStr = this.getRemoteAddress().toString();
            this.remoteAddrStr = this.remoteAddrStr.substring(1, this.remoteAddrStr.length());
            this.server.writeToAll(this.remoteAddrStr + " has connected.");
            String line = this.socketIn.readLine();

            while (line != null) {
                debug("client says '" + line + "'");
                
                // If it's a quit command, we remove the client from the server and exit
                if (line.compareToIgnoreCase("\\quit") == 0) {
                    if (this.server.remove(this.getRemoteAddress())) {
                        this.finalize();
                        return;
                    }
                }
                this.remoteAddrStr = this.getRemoteAddress().toString();
                this.remoteAddrStr = this.remoteAddrStr.substring(1, this.remoteAddrStr.length());
                this.server.writeToAll(this.remoteAddrStr + ":" + line);
                line = this.socketIn.readLine();
                //Write Clients to Array
                writeToArray(this.remoteAddrStr, line);
            }
        }
        catch (Exception e) {
            debug("Exception (run): " + e.getMessage());
        }
    }

    public void writeToArray(String address, String data)
    {
        
    }

    public void writeClientP(String address, String data)
    {
        Main.chatServerGUI.writeClient(address);
        Main.chatServerGUI.writeClientCoord(data);
    }

    public void write(String msg) {
        try {
            this.socketOut.write(msg + "\u0000");
            this.socketOut.flush();
        }
        catch (Exception e) {
            debug("Exception (write): " + e.getMessage());
        }
    }

    protected void finalize() {	 
        try {
            this.socketIn.close(); 
            this.socketOut.close();
            this.socket.close();
            debug("connection closed");
        }
        catch (Exception e) {
            debug("Exception (finalize): " + e.getMessage());
        }
    }
}